package com.api.ecustom.common;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import com.alibaba.fastjson.JSONObject;

import ecustom.common.AbstractWebAction;
import ecustom.common.util.PropUtil;

/**
 * WebApi公共Action。
 * @author 曹水涛
 */
@Path("/ecustom/common")
public class CommonAction extends AbstractWebAction {
	
	private static final Map<String, Object> actions = new HashMap<>();
	
	@Path("/{type}/{module}/{action}")
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	public JSONObject get(@PathParam("type") String type
			, @PathParam("module") String module
			, @PathParam("action") String action
			, @Context HttpServletRequest req
			, @Context HttpServletResponse res) {
		return action(type, module, action, req, res);
	}
	
	@Path("/{type}/{module}/{action}")
	@POST
	@Produces(MediaType.APPLICATION_JSON)
	public JSONObject post(@PathParam("type") String type
			, @PathParam("module") String module
			, @PathParam("action") String action
			, @Context HttpServletRequest req
			, @Context HttpServletResponse res) {
		return action(type, module, action, req, res);
	}
	
	public JSONObject action(@PathParam("type") String type
			, @PathParam("module") String module
			, @PathParam("action") String action
			, @Context HttpServletRequest request
			, @Context HttpServletResponse response) {
		try {
			if (log.isDebugEnabled()) {
				String pathInfo = request.getPathInfo();
				log.debug("requestUrl: " + pathInfo);
				Enumeration<?> pNames = request.getParameterNames();
				while (pNames.hasMoreElements()) {
					String name = (String)pNames.nextElement();
					log.debug("param." + name + ": " + request.getParameter(name));
				}
				pNames = request.getHeaderNames();
				while (pNames.hasMoreElements()) {
					String name = (String)pNames.nextElement();
					log.debug("header." + name + ": " + request.getHeader(name));
				}
			}
			String pkgName = PropUtil.getString("api.package." + type);
			if (pkgName == null || pkgName.isEmpty())
				pkgName = "ecustom." + type + ".api";
			String clzName = pkgName + "." + module.substring(0, 1).toUpperCase() + module.substring(1) + "Action";
			if (!actions.containsKey(clzName)) {
				actions.put(clzName, Class.forName(clzName).newInstance());
			}
			Method method = actions.get(clzName).getClass().getMethod(action, HttpServletRequest.class, HttpServletResponse.class);
			JSONObject resObj = (JSONObject)method.invoke(actions.get(clzName), request, response);
			if (log.isDebugEnabled()) log.debug("action() resJson: " + resObj);
			return resObj;
		} catch (IllegalAccessException | InvocationTargetException e) {
			log.error("action() action: " + action, e.getCause());
			return err(e.getCause().toString());
		} catch (Exception e) {
			log.error("action() action: " + action, e);
			return err(e.toString());
		} 
	}
}